local PartnerLevelPanel = app.mvc.createPanelClass(...)
local PartnerUseExpPanel = import(".PartnerUseExpPanel")
--data =
function PartnerLevelPanel.createWithData(data, node)
    assert(node ~= nil, "PartnerLevelPanel node is nil")
    local sprite = PartnerLevelPanel.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)

    return sprite
end


function PartnerLevelPanel.create(node)
    return PartnerLevelPanel.createWithData(nil, node)
end


function PartnerLevelPanel:init(data)
   
    self.super.init(self, data)

    local sp = cc.Sprite:createWithSpriteFrameName(Res.PartnerLevelProgressPath)

    self._progressTime = cc.ProgressTimer:create( sp )
    self._progressTime:setType( cc.PROGRESS_TIMER_TYPE_RADIAL )
    self._progressTime:setMidpoint(display.ANCHOR_POINTS[1])
    self._ui["exp"]:addChild(self._progressTime)

    self._lvWidth = self._ui["levelPanel"]:getContentSize().width
    self._textWidth = self._ui["levelPanel/text1"]:getContentSize().width

    self._ui["bg_exp/bg4"]:setTouchEnabled(true)

    self._ui["btn_up"]:addClickEventListener(function()
        if not self._data  then
            return
        end
        if self._data:isSpReward() then
            display.pushToast(L("partnerCantUseExpItem"))
            return
        end
        if not self._data:isHave() then
            display.pushToast(L("PartnerNotHave"))
            return
        end
        EM:notify(ViewEvent.Partner_OpenUseExpPanel)
    end)
end

function PartnerLevelPanel:refresh()
    if not self._data then
        return
    end
    local data = self._data
    local lv = data:getLevel()
    self._ui["levelPanel/lv_num"]:setString(lv)
    local maxExp = GD:queryPartnetLevelUpExpConfig(lv)
    local curExp = data:getExp()
    self._ui["exp_num"]:setString(string.format("%s/%s", curExp, maxExp))
    self._progressTime:setPercentage(math.floor( curExp * 100/ maxExp))
    
    if data:isSpReward() then
        self._ui["text2"]:setString(L("partnerCantUseExpItem"))
    else
        self._ui["text2"]:setString(L("partnerUseExpItem"))
    end
    -- local lvNumWidth = self._ui["levelPanel/lv_num"]:getContentSize().width
    -- local offsetx = (self._lvWidth - lvNumWidth - self._textWidth)/2
    -- self._ui["levelPanel/lv_num"]:setPositionX(offsetx)
    -- self._ui["levelPanel/text1"]:setPositionX(offsetx + lvNumWidth + 2)
end

function PartnerLevelPanel:setProgress(v)
    self._progressTime:setPercentage(v)
end

return PartnerLevelPanel